SystemVerilog Casting |
您所在的位置:网站首页 › verilog const int › SystemVerilog Casting |
Casting
Table of Contents In Manufacturing, Casting is a process in which liquid metal is converted into the desired object. Similarly, SystemVerilog casting means the conversion of one data type to another datatype. During value or variable assignment to a variable, it is required to assign value or variable of the same data type. Some situations need assignment of different data type, in such situations, it is necessary to convert data type and assign. Otherwise, the assignment of different data type results in a compilation error. The method of data type conversion is called casting. In systemVerilog, there are two types of casting, ![]() ![]() Please enable JavaScript Casting in Java SystemVerilog static casting is not applicable to OOP Static casting converts one data type to another compatible data types (example string to int) As the name says ‘Static’, the conversion data type is fixed Static casting will be checked during compilation, so there won’t be run-time checking and error reporting Casting is applicable to value, variable or to an expression A data type can be changed by using a cast ( ‘ ) operation The vale/variable/expression to be cast must be enclosed in parentheses or within concatenation or replication braces Static casting exampleIn the below example, the real type is converted into int type. i.e multiplication of two real numbers results in real value, the result is converted into int and then assigned to a variable of int type. Note: the casting is applied to expression here. module casting; real r_a; int i_a; initial begin r_a = (2.1 * 3.2); //real to integer conversion i_a = int'(2.1 * 3.2); //or i_a = int'(r_a); $display("real value is %f",r_a); $display("int value is %d",i_a); end endmoduleSimulator Output real value is 6.720000 int value is 7Click to execute on ![]() Let’s see how we can use the casting, It is always legal to assign a child class variable to a variable of a class higher in the inheritance tree (parent class). parent_class = child_class; //allowed It is never legal to directly assign a super-class (parent class) variable to a variable of one of its subclasses (child class). child_class = parent_class; //not-allowed
Though parent_class is pointing to the child_class, we will get a compilation error saying its not compatible type for the assignment. This we can over come by make use of $cast method, i.e, $cast(child_class,parent_class);
In the above parent class assignment with child class example. type of parent class is changing dynamically i.e on declaration it is of parent class type, on child class assignment it is of child class type. Parent class handle during $cast execution is considered for the assignment, so it referred to as dynamic casting. Dynamic Casting examples assigning child class handle to parent class handle class parent_class; bit [31:0] addr; function display(); $display("Addr = %0d",addr); endfunction endclass class child_class extends parent_class; bit [31:0] data; function display(); super.display(); $display("Data = %0d",data); endfunction endclass module inheritence; initial begin parent_class p=new(); child_class c=new(); c.addr = 10; c.data = 20; p = c; //assigning child class handle to parent class handle c.display(); end endmoduleSimulator Output Addr = 10 Data = 20Click to execute on ![]() This assignment is invalid, it leads to a compilation error. class parent_class; bit [31:0] addr; function display(); $display("Addr = %0d",addr); endfunction endclass class child_class extends parent_class; bit [31:0] data; function display(); super.display(); $display("Data = %0d",data); endfunction endclass module inheritence; initial begin parent_class p=new(); child_class c=new(); c.addr = 10; c.data = 20; c = p; //assigning child class handle to parent class handle c.display(); end endmoduleSimulator Output ![]() Click to execute on assigning parent class handle (which is pointing to child class handle) to child class handle, leads to compile error. class parent_class; bit [31:0] addr; function display(); $display("Addr = %0d",addr); endfunction endclass class child_class extends parent_class; bit [31:0] data; function display(); super.display(); $display("Data = %0d",data); endfunction endclass module inheritence; initial begin parent_class p; child_class c=new(); child_class c1; c.addr = 10; c.data = 20; p = c; //p is pointing to child class handle c. c1 = p; //type check fails during compile time. c1.display(); end endmoduleSimulator Output "c1 = p;" Expression 'p' on rhs is not a class or a compatible class and hence cannot be assigned to a class handle on lhs. Please make sure that the lhs and rhs expressions are compatible.Click to execute on ![]() In the above example, assigning parent class handle (which is pointing to child class handle) to child class handle is valid but compilation error is observed. During the compile time, as the handle of p is of parent class type which leads to compile error. With the use of $cast(), type check during compile time can be skipped. class parent_class; bit [31:0] addr; function display(); $display("Addr = %0d",addr); endfunction endclass class child_class extends parent_class; bit [31:0] data; function display(); super.display(); $display("Data = %0d",data); endfunction endclass module inheritence; initial begin parent_class p; child_class c=new(); child_class c1; c.addr = 10; c.data = 20; p = c; //p is pointing to child class handle c. $cast(c1,p); //with the use of $cast, type chek will occur during runtime c1.display(); end endmoduleSimulator Output Addr = 10 Data = 20Click to execute on ❮ Previous Next ❯ |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |